﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Sql;
using System.Data.Common;
using System.Data.SqlClient;

namespace sccf
{
    public class ConfigurarConvocatoriaModel
    {
        static SqlConnection connection = new SqlConnection(Vars.connectionString);
        public static List<string> Registrar(string comentario, DateTime fechaini, DateTime fechafin)
        {

            connection.Open();
            SqlCommand cmd1 = new SqlCommand("ObtenerCodConvConvoc", connection);
            SqlDataReader Idreader = cmd1.ExecuteReader();
            Idreader.Read();
            string id = Idreader[0].ToString();
            
            SqlParameter Coment = new SqlParameter("@coment", comentario);
            SqlParameter FechaIni = new SqlParameter("@FechaIni", fechaini);
            SqlParameter FechaFin = new SqlParameter("@FechaFin", fechafin);

            SqlCommand cmd = new SqlCommand("ConfigurarConvoc", connection);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.Parameters.Add(Coment);
            cmd.Parameters.Add(FechaIni);
            cmd.Parameters.Add(FechaFin);

            SqlDataReader correos = cmd.ExecuteReader();
            List<string> correoss = new List<string>();
            correoss.Add(id);
            while (correos.Read())
            {
                correoss.Add(correos["email"].ToString());
                correoss.Add(correos["email2"].ToString());
            }
            connection.Close();
            return correoss;
        }

        public static int Consultar()
        {
            connection.Open();
            SqlCommand cmd = new SqlCommand("ConsultaAgendaNoCreada", connection);
            SqlCommand cmd2 = new SqlCommand("ConsultarActiva", connection);
            SqlCommand cmd3 = new SqlCommand("ConsultarAgendaCreada", connection);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd2.CommandType = System.Data.CommandType.StoredProcedure;
            cmd3.CommandType = System.Data.CommandType.StoredProcedure;
            SqlDataReader agendas = cmd.ExecuteReader();
            //cmd.Cancel();
            SqlDataReader Activa = cmd2.ExecuteReader();
            SqlDataReader Creada = cmd3.ExecuteReader();
            bool nocreada= Convert.ToBoolean(agendas.HasRows.ToString());
            bool activa = Convert.ToBoolean(Activa.HasRows.ToString());
            bool creada = Convert.ToBoolean(Creada.HasRows.ToString());
            connection.Close();
            if (creada)
            {
                return 0;
            }
            else if (nocreada)
                return 1;
            else if (activa)
                return 2;
            else
                return 3;
        }

        public static SqlDataReader puntos()
        {
            SqlConnection connection = new SqlConnection(Vars.connectionString);
            connection.Open();
            SqlCommand cmd = new SqlCommand("ObtenerPuntosAgenda", connection);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            return cmd.ExecuteReader();
        }
    }
}